﻿using LogSystem;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Utils;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class NpcFlee : AIBaseNode
    {
        // 逃跑时间
        public BBParameter<float> escapeDuration = 5f;
        
        protected override void OnExecute()
        {
            _entity.seeker.SwitchSpeedStrategy(SpeedStrategyType.Const);
            Vector3 pos = _transform.position;
            Vector3 forward = _transform.forward;
            PathPointData point = LibertyAIUtils.TrafficManager.PathManager.GetNearestNavPoint(pos, forward, out PathDirection pathDirection);
            point = LibertyAIUtils.TrafficManager.PathManager.RandomEscapeDestination(pos);
            float offsetFactor = _entity.data.pathOffsetFactor;

            if (point!=null)
            {
                Vector3 destination = AIGraphUtils.GetNpcDestinationFormPoint(point, GetPosition(), offsetFactor);
                
                _entity.data.destination = destination;
                _entity.data.pathId = point.pathId;
                _entity.data.pathPointIndex = point.pointIndex;
                SetDestination(destination);
            }
            else
            {
                Debug.LogError(LogModule.LibertySceneBattle, $"没有找到合适的逃跑目标点");
                EndAction(true);
            }
        }
        
        protected override void OnUpdate()
        {
            base.SetTransformData();
            float dis = Vector3.Distance(_entity.data.destination, GetPosition());
            if (_entity.seeker.ReachedEndOfPath() || dis < 1.6f || elapsedTime >= escapeDuration.value) // todo 应该获取到玩家RVO 和自身 RVO半径
            {
                EndAction(true);
            }
            
        }
    }
}